package my.util;

/**
 * <p>
 * 双向链表
 * </p>
 *
 * @author AJun
 * @since 2020/8/17
 */
@SuppressWarnings("ALL")
public class LinkedList<E> extends AbstractList<E> implements List<E> {

    /**
     * 头节点
     */
    private Node<E> first;

    /**
     * 尾节点
     */
    private Node<E> last;

    /**
     * 节点
     */
    private static class Node<E> {
        /**
         * 节点数据
         */
        E element;

        /**
         * 指向上一个节点
         */
        Node<E> prev;

        /**
         * 指向下一个节点
         */
        Node<E> next;

        public Node(E element, Node<E> prev, Node<E> next) {
            this.element = element;
            this.prev = prev;
            this.next = next;
        }
    }

    /**
     * 根据传入的索引返回该索引的元素
     */
    private Node<E> node(int index) {
        rangeCheck(index);

        Node<E> node;
        if (index < (size >> 1)) {
            // 查找的节点靠近左边
            node = first;
            for (int i = 0; i < index; i++)
                node = node.next;
        } else {
            // 查找的节点靠近右边
            node = last;
            for (int i = size - 1; i > index; i--)
                node = node.prev;
        }
        return node;
    }

    @Override
    public int indexOf(E element) {
        Node<E> node = first;
        if (element == null) {
            for (int i = 0; i < size; i++) {
                if (node.element == null)
                    return i;
                node = node.next;
            }
        } else {
            for (int i = 0; i < size; i++) {
                if (element.equals(node.element))
                    return i;
                node = node.next;
            }
        }
        return -1;
    }

    @Override
    public E get(int index) {
        return node(index).element;
    }

    @Override
    public E set(int index, E element) {
        Node<E> node = node(index);
        E oldElement = node.element;
        node.element = element;
        return oldElement;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);

        if (index == size) {
            // 尾插
            Node<E> oldLast = last;
            last = new Node<>(element, oldLast, null);
            if (oldLast == null) // 链表为空
                first = last;
            else
                oldLast.next = last;
        } else {
            // 任意位置添加
            Node<E> next = node(index); // 后一个节点
            Node<E> prev = next.prev;   // 前一个节点
            Node<E> node = new Node<>(element, prev, next); // 要插入的节点
            next.prev = node;

            if (prev == null) // index == 0
                first = node;
            else // index != 0
                prev.next = node;
        }
        size++;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        return unlink(node(index));
    }

    @Override
    public boolean remove(E element) {
        if (element == null) {
            for (Node<E> node = first; node != null; node = node.next) {
                if (node.element == null) {
                    unlink(node);
                    return true;
                }
            }
        } else {
            for (Node<E> node = first; node != null; node = node.next) {
                if (element.equals(node.element)) {
                    unlink(node);
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 删除指定节点
     *
     * @param node 指定节点
     * @return 被删除的节点的数据
     */
    private E unlink(Node<E> node) {
        final E element = node.element;
        final Node<E> prev = node.prev;
        final Node<E> next = node.next;

        if (prev == null) { // index == 0
            first = next;
        } else {
            prev.next = next;
            node.prev = null;
        }

        if (next == null) { // index = size - 1
            last = prev;
        } else {
            next.prev = prev;
            node.next = null;
        }

        node.element = null;
        size--;
        return element;
    }

    @Override
    public void clean() {
        first = null;
        last = null;
        size = 0;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        Node<E> node = first;
        for (int i = 0; i < size; i++) {
            if (i > 0) sb.append(", ");
            sb.append(node.element);
            node = node.next;
        }
        sb.append("]");
        return sb.toString();
    }

}
